package com.notetool.util.config;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AppConfig
{
    
    ////////////////////
    // Constants
    private static final String configFile = "/config/config.xml";
    
	////////////////////
    // Static members
    private static ConfigSet rootNode = null;
    
    ////////////////////
    // Public static methods
    public static ConfigSet getConfig ()
    {
        if (rootNode == null)
            rootNode = parseFile (configFile);
        
        return rootNode;
    }
    
    private static ConfigSet parseFile(String path)
    {
        try
        {
            // Get document loader
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            
            // Get InputStream for the config file
            InputStream str = AppConfig.class.getResourceAsStream(path);
            
            // Parse with DOM
            Document dom = db.parse(str);
            
            // Instanciate the root-level ConfigSet with the Document Node
            return new ConfigSet(dom);
        }
        catch (Exception ex)
        {
            // Throw unchecked exception
            throw new ConfigLoadingException ();
        }
    }
	
}